package cn.yinyu.queue.module.ecg.controller.admin.queue;

import cn.yinyu.queue.framework.common.exception.ErrorCode;
import cn.yinyu.queue.module.ecg.service.config.EcgConfigService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.yinyu.queue.framework.common.pojo.PageParam;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.pojo.CommonResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;

import static cn.yinyu.queue.framework.common.exception.enums.GlobalErrorCodeConstants.SUCCESS;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.error;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.success;

import cn.yinyu.queue.framework.excel.core.util.ExcelUtils;

import cn.yinyu.queue.framework.apilog.core.annotation.ApiAccessLog;
import static cn.yinyu.queue.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.yinyu.queue.module.ecg.controller.admin.queue.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.queue.QueueDO;
import cn.yinyu.queue.module.ecg.service.queue.QueueService;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.annotation.Resource;

@Tag(name = "管理后台 - 排队")
@RestController
@RequestMapping("/ecg/queue")
@Validated
public class QueueController {

    @Resource
    private QueueService queueService;

    @Resource
    private EcgConfigService ecgConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建排队")
    @PreAuthorize("@ss.hasPermission('ecg:queue:create')")
    public CommonResult<Integer> createqueue(@Valid @RequestBody QueueSaveReqVO createReqVO) {
        return success(queueService.createqueue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排队")
    @PreAuthorize("@ss.hasPermission('ecg:queue:update')")
    public CommonResult<Boolean> updatequeue(@Valid @RequestBody QueueSaveReqVO updateReqVO) {
        queueService.updatequeue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排队")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ecg:queue:delete')")
    public CommonResult<Boolean> deletequeue(@RequestParam("id") Integer id) {
        queueService.deletequeue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得排队")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:queue:query')")
    public CommonResult<QueueRespVO> getqueue(@RequestParam("id") Integer id) {
        QueueDO queue = queueService.getqueue(id);
        return success(BeanUtils.toBean(queue, QueueRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得排队分页")
    @PreAuthorize("@ss.hasPermission('ecg:queue:query')")
    public CommonResult<PageResult<QueueRespVO>> getqueuePage(@Valid QueuePageReqVO pageReqVO) {
        PageResult<QueueDO> pageResult = queueService.getqueuePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QueueRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排队 Excel")
    @PreAuthorize("@ss.hasPermission('ecg:queue:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportqueueExcel(@Valid QueuePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QueueDO> list = queueService.getqueuePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "排队.xls", "数据", QueueRespVO.class,
                        BeanUtils.toBean(list, QueueRespVO.class));
    }

    @GetMapping("/bed-open")
    @Operation(summary = "工位开通")
    @PreAuthorize("@ss.hasPermission('ecg:queue:bedcontrol')")
    public CommonResult<Integer> bedOpen(
        @RequestParam("roomId") Long roomId,
        @RequestParam("roomName") String roomName,
        @RequestParam("bedNo") String bedNo)
    {
        ErrorCode result = queueService.startBedOpen(roomId, roomName, bedNo);
        if (result.equals(SUCCESS))
            return success(0);

        return error(result);
    }

    @GetMapping("/bed-close")
    @Operation(summary = "工位关闭")
    @PreAuthorize("@ss.hasPermission('ecg:queue:bedcontrol')")
    public CommonResult<Integer> bedClose(
        @RequestParam("roomId") Long roomId,
        @RequestParam("bedNo") String bedNo)
    {
        ErrorCode result = queueService.startBedClose(roomId, bedNo);
        if (result.equals(SUCCESS))
            return success(0);

        return error(result);
    }

    @GetMapping("/patient-jump")
    @Operation(summary = "插队")
    @Parameter(name = "patId", description = "患者编号", required = true, example = "B2")
    @PreAuthorize("@ss.hasPermission('ecg:queue:jump')")
    public CommonResult<String> queueJump(
            @RequestParam("patId") String patId,
            @RequestParam("jumpFlag") Byte jumpFlag)
    {
        Integer result = queueService.patientJump(patId, jumpFlag);
        if (null == result || 0 == result)
            return error( new ErrorCode(201, "找不到患者") );

        return success("success");
    }

    @GetMapping("/reset-scheduler")
    @Operation(summary = "重置调度器")
    @PreAuthorize("@ss.hasPermission('ecg:queue:setting')")
    public CommonResult<Integer> resetScheduler() {
        ecgConfigService.resetScheduler();
        return success(0);
    }

}
